package com.jvm.interrupt;

import java.util.concurrent.TimeUnit;

// 通过一个变量控制线程中断
public class Demo1 {

    // volatile 控制了变量在多线程中的可见性
    public volatile static boolean exit = false;

    public static class T extends Thread {
        @Override
        public void run() {
            while (true) {
                // 循环处理业务
                if (exit) {
                    break;
                }
            }
        }
    }

    public static void setExit() {
        exit = true;
    }

    public static void main(String[] args) throws InterruptedException {
        T t = new T();

        t.start();
        TimeUnit.SECONDS.sleep(3);
        setExit();
    }


}
